package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.utils.Base64Coder;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

/**
 * Created by Joney on 2015/9/9.
 * 视频申请
 */
public class ApplyVideoActivity extends BaseActivity implements OnClickListener {

    private LinearLayout ll_nomarl;//未添加封皮
    private ImageView iv_small_img;//缩放图
    private ImageView iv_img; // 整体背景图
    private TextView tv_none;// 是否添加封面
    private EditText et_name, et_content, et_url; //名字和描述 ,视频地址
    //要上传的图片数据
    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int RESULT_REQUEST_CODE = 2;
    private String dataIntent = "";//封面图片的Base 字符串
    private Button btn_cancel, btn_apply; //申请，取消

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_apply);
        setNavBtn(R.mipmap.back, 0);
        setTitle("视频申请");
        findById();
    }

    private void findById() {

        ll_nomarl = (LinearLayout) findViewById(R.id.ll_nomarl);
        iv_small_img = (ImageView) findViewById(R.id.iv_small_img);
        iv_img = (ImageView) findViewById(R.id.iv_img);
        tv_none = (TextView) findViewById(R.id.tv_none);
        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(this);
        btn_apply = (Button) findViewById(R.id.btn_apply);
        btn_apply.setOnClickListener(this);
        et_name = (EditText) findViewById(R.id.et_name);
        et_content = (EditText) findViewById(R.id.et_content);
        et_url = (EditText) findViewById(R.id.et_url);


        ll_nomarl.setOnClickListener(this);
        iv_small_img.setOnClickListener(this);
    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {
        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_nomarl:
                break;

            case R.id.iv_small_img:
                Intent intent2 = new Intent();
                intent2.setType("image/*"); // 设置文件类型
                intent2.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent2,
                        IMAGE_REQUEST_CODE);
                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_apply:
                toApply();
                break;
            default:

                break;


        }
    }

    private void toApply() {
        if (TextUtils.isEmpty(et_name.getText().toString().trim())) {
            Toast.makeText(ApplyVideoActivity.this, "名称不能为空！", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(et_content.getText().toString().trim())) {
            Toast.makeText(ApplyVideoActivity.this, "描述不能为空！", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(dataIntent.trim())) {
            Toast.makeText(ApplyVideoActivity.this, "封面还未设置，请您设置封面！", Toast.LENGTH_SHORT).show();
            return;
        } else if (TextUtils.isEmpty(et_url.getText().toString().trim())) {
            Toast.makeText(ApplyVideoActivity.this, "视频地址还未输入，请添加后提交！", Toast.LENGTH_SHORT).show();
            return;
        }


        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        try {
            object.put("show_content_url", et_url.getText().toString().trim());
            object.put("user_id", user_id);
            object.put("show_name", et_name.getText().toString().trim());
            object.put("show_reamk", et_content.getText().toString().trim());
            object.put("show_cove_url", dataIntent);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("8040", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {

                if (array == null) {
                    Log.e("code", code.toString());
                    Log.e("message", message.toString());
                } else {
                    if ("200".equals(code.toString().trim())) {
                        Toast.makeText(ApplyVideoActivity.this, "视频申请成功！", Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }
            }
        });

    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param data
     */
    @SuppressWarnings("deprecation")
    private void getImageToView(Intent data) {
        Bundle extras = data.getExtras();
        ByteArrayOutputStream stream = null;
        Bitmap photo = null;
        Drawable drawable = null;
        String picStr = null;
        byte[] bytes = null;
        if (extras != null) {

            photo = extras.getParcelable("data");
            drawable = new BitmapDrawable(photo);
            iv_img.setVisibility(View.VISIBLE);

            iv_small_img.setImageDrawable(drawable);
            iv_img.setImageDrawable(drawable);
            tv_none.setText("已添加封面");
            stream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
            bytes = stream.toByteArray();
            // 将图片流以字符串形式存储下来
            picStr = new String(Base64Coder.encodeLines(bytes));
            dataIntent = picStr;

        }

    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, RESULT_REQUEST_CODE);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_CANCELED) {

            if (resultCode != RESULT_CANCELED) {

                switch (requestCode) {
                    case IMAGE_REQUEST_CODE:
                        startPhotoZoom(data.getData());

                        break;
                    case RESULT_REQUEST_CODE:
                        if (data != null) {
                            getImageToView(data);
                        }
                        break;
                }

            }

        }
        switch (resultCode) {
            case 200:
                String file_path = data.getStringExtra("file_path");
                Log.e("file_path", file_path);

                break;
        }


    }
}
